package com.zxbit.flash.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.utils.ToastUtils;

public class GrantPermissionDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private CheckBox checkAgreement;
    private TextView tvAgreement;
    private TextView confirm;
    private TextView cancel;
    private GrantPermissionDialogCallBack callBack;

    public GrantPermissionDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public GrantPermissionDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected GrantPermissionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        View view = View.inflate(context, R.layout.layout_grant_permission_dialog, null);
        checkAgreement = view.findViewById(R.id.check_agreement);
        tvAgreement = view.findViewById(R.id.tv_agreement);
        confirm = view.findViewById(R.id.confirm_button);
        cancel = view.findViewById(R.id.cancel_button);
        setContentView(view);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        tvAgreement.setOnClickListener(this);
        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_agreement:
                checkAgreement.setChecked(true);
                break;
            case R.id.confirm_button:
                if (!checkAgreement.isChecked()) {
                    ToastUtils.showShort((Activity) context, context.getResources().getString(R.string.privacy_agreement_not_read));
                    return;
                }
                if (callBack != null)
                    callBack.onConfirm();
                break;
            case R.id.cancel_button:
                if (callBack != null)
                    callBack.onCancel();
                dismiss();
                break;
        }
    }

    public void setOnClickListener(GrantPermissionDialogCallBack callBack) {
        this.callBack = callBack;
    }

    public interface GrantPermissionDialogCallBack {
        void onConfirm();

        void onCancel();
    }
}
